#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/types.h> 
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>

#include "cgic.h"

struct env_info
{
	uint8_t head[3];	 //标识位st:
	uint8_t type;		 //数据类型
	uint8_t snum;		 //仓库编号
	uint8_t temp[2];	 //温度	
	uint8_t hum[2];		 //湿度
	uint8_t x;			 //三轴信息
	uint8_t y;			 
	uint8_t z;			 
	uint32_t ill;		 //光照
	uint32_t bet;		 //电池电量
	uint32_t adc; 		 //电位器信息
};

struct msg{
	pid_t pid;
	struct env_info buf;
};

void myfun(int arg)
{
	return;
}

#define N 1024 // 共享内存大小（其实只需要知道共享内存首地址，所以在这大小无所谓）

int cgiMain()
{
	key_t key;
//	pid_t pid;
//	int semid;
	int shmid;
	struct msg *p = NULL;
//	struct env_info *shm_buf = NULL;

	struct env_info env_tmp;

	if((key = ftok("/app", 'i')) < 0)
	{
		perror("ftok");
		exit(1);
	}
	
	if((shmid = shmget(key, N, 0666 )) == -1)
	{
		perror("shmget");
		exit(1);
	}
	
	if((p = (struct msg*)shmat(shmid, NULL, 0)) == (void*)-1)
	{
		perror("shmat");
		exit(1);
	}

	//env_tmp = *shm_buf;
//	signal(SIGUSR2,myfun);
//	pid = p->pid;
//	p->pid = getpid();
//	kill(pid,SIGUSR1);
	env_tmp = p->buf;
	
//	while(1)
//	{
//		pause();
	//	env_tmp = p->buf;
	//	printf("recv msg:%s",p->buf);
	//	printf("%d\n",p->buf.x);
	//	kill(pid,SIGUSR1);
	
	cgiHeaderContentType("text/html"); 
	fprintf(cgiOut, "<HTML><HEAD>"); 
	fprintf(cgiOut, "<TITLE>当前实时数据</TITLE>");  
	fprintf(cgiOut, "<style type=\"text/css\"> body,td,th {font-size: 12px;margin-left: 0px;margin-top: 0px;}</style></HEAD>"); 
	fprintf(cgiOut, "<BODY BGCOLOR=\"#F8F9FA\">"); 

	fprintf(cgiOut, "<meta http-equiv=\"refresh\" content=\"1\">");

	fprintf(cgiOut, "<table width=\"100%%\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\">");
	// 实时数据表头
	fprintf(cgiOut, 	"<tr bgcolor=\"#DEE4E6\">");
	
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">名称</td>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">最新数值</td>");
	fprintf(cgiOut, 	"</tr>");
	
	// 温度采集	
	fprintf(cgiOut, 	"<tr bgcolor=\"#f2f2f2\">");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">环境温度</td>");
	fprintf(cgiOut, 	"<td height=\"30\" align=\"center\" valign=\"middle\">%d℃</td>", env_tmp.temp[0]);
	fprintf(cgiOut, 	"</tr>");
	
	// 湿度采集
	fprintf(cgiOut, 	"<tr>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">环境湿度</td>");
	fprintf(cgiOut, 	"<td height=\"30\" align=\"center\" valign=\"middle\">%d％</td>", env_tmp.hum[0]);
	fprintf(cgiOut, 	"</tr>");
	
	// 光照采集
	fprintf(cgiOut, 	"<tr bgcolor=\"#f2f2f2\">");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">环境光照</td>");
	fprintf(cgiOut, 	"<td height=\"30\" align=\"center\" valign=\"middle\">%dlux</td>", env_tmp.ill);
	fprintf(cgiOut, 	"</tr>");
	
	// 电量采集
	fprintf(cgiOut, 	"<tr>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">电池电量</td>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">%dV</td>", env_tmp.bet);
	fprintf(cgiOut, 	"</tr>");
	
	// ADC采集	
	fprintf(cgiOut, 	"<tr bgcolor=\"#f2f2f2\">");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">模拟电压</td>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">%dV</td>", env_tmp.adc);
	fprintf(cgiOut, 	"</tr>");
	
	fprintf(cgiOut, 	"<tr>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\">设备姿态</td>");
	fprintf(cgiOut, 		"<td height=\"30\" align=\"center\" valign=\"middle\"><table width=\"200\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">");
	fprintf(cgiOut, 			"<tr align=\"center\" valign=\"middle\">");
	fprintf(cgiOut, 					"<td width=\"25%%\">&nbsp;</td>");
	fprintf(cgiOut, 					"<td width=\"25%%\">X</td>");
	fprintf(cgiOut, 					"<td width=\"25%%\">Y</td>");
	fprintf(cgiOut, 					"<td width=\"25%%\">Z</td>");
	fprintf(cgiOut, 			"</tr>");
	fprintf(cgiOut, 			"<tr align=\"center\" valign=\"middle\">");
	fprintf(cgiOut, 					"<td>原始值</td>");
	fprintf(cgiOut, 					"<td>%d</td>", env_tmp.x);
	fprintf(cgiOut, 					"<td>%d</td>", env_tmp.y);
	fprintf(cgiOut, 					"<td>%d</td>", env_tmp.z);
	fprintf(cgiOut, 			"</tr>");
	fprintf(cgiOut, 		"</table></td>");
	fprintf(cgiOut, 	"</tr>");
	fprintf(cgiOut, "</table>");

	fprintf(cgiOut, "<script type=\"text/jscript\">");
	fprintf(cgiOut, "setTimeout(\"self.close()\", 3000)");
	fprintf(cgiOut, "</script>");
	fprintf(cgiOut, "</BODY>"); 
	fprintf(cgiOut, "</HTML>");
	fflush(stdout);
//	kill(pid,SIGUSR1);
//	}
	return 0;
}





